package com.mf.valid.validator;

import com.mf.valid.ValidListValue;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

import java.util.HashSet;

/**
 * @Author: lmf
 * @Create: 2025/4/4 16:28
 * @module
 */
public class ValidatorListValueConstraint implements ConstraintValidator<ValidListValue, Integer> {
    private final HashSet<Integer> set = new HashSet<>();

    @Override
    public void initialize(ValidListValue constraintAnnotation) {
        for (int i : constraintAnnotation.val()) {
            set.add(i);
        }
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }
        return set.contains(value);
    }
}
